//
//  HomeSelectionViewController.m
//  GiftNote
//
//  Created by lanou on 15/8/12.
//  Copyright (c) 2015年 GN. All rights reserved.
//

#import "HomeSelectionViewController.h"
#import "HomeHeader.h"
#import "HomeGiftCell.h"
#import "HomeSelectionHeadView.h"
#import "LORequestManger.h"
#import "HomeWheelModel.h"
#import "HomeBeautyModel.h"
#import "MJRefresh.h"
#import "HomeBaseDetailViewController.h"


typedef NS_ENUM(NSInteger, RefreshDownLoadType) {
    RefreshDownLoadTypeUp,
    RefreshDownLoadTypeDown
};

@interface HomeSelectionViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *cellArray;
@property (nonatomic, strong)HomeSelectionHeadView *headView;
@property (nonatomic, strong)NSMutableArray *wheelArray;
@property (nonatomic, strong)NSMutableArray *beautyArray;
@property (nonatomic, strong)HomeBaseDetailViewController *baseDVC;

@property (nonatomic, assign)int num;
@property (nonatomic, assign)RefreshDownLoadType refreshDownLoadType;

@end

@implementation HomeSelectionViewController

- (NSMutableArray *)cellArray
{
    if (_cellArray == nil) {
        self.cellArray = [NSMutableArray array];
    }
    return _cellArray;
}

- (NSMutableArray *)wheelArray
{
    if (_wheelArray == nil) {
        
        self.wheelArray = [NSMutableArray array];
        
    }
    return _wheelArray;
}

- (NSMutableArray *)beautyArray
{
    if (_beautyArray == nil) {
        
        self.beautyArray = [NSMutableArray array];
        
    }
    return _beautyArray;
}

- (void)dealloc
{
    NSLog(@"select消失啦");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _num = 0;
    
    [self creatTableView];
    [self readScrollData];
    [self readBeautyData];
    [self readCellData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - 创建Table View

- (void)creatTableView
{
    CGFloat height = self.navigationController.navigationBar.frame.size.height;
    _tableView  = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kHomeWidth, kHomeHeight - height - 44) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 273 * (kHomeWidth - 20) / 600 + 15;
    [self.view addSubview:_tableView];
    
    [_tableView registerClass:[HomeGiftCell class] forCellReuseIdentifier:@"HomeGiftCell"];
    
    _headView = [[HomeSelectionHeadView alloc]initWithFrame:CGRectMake(0, 0, kHomeWidth, kHomeHeight / 4 + kHomeWidth / 4)];
    _headView.naVC = _naVC;
    _tableView.tableHeaderView = _headView;
    
    
    [_tableView addGifHeaderWithRefreshingTarget:self refreshingAction:@selector(headDownLoad)];
    
    [_tableView addGifFooterWithRefreshingTarget:self refreshingAction:@selector(footDownLoad)];
}


#pragma mark - TableViewDelegate/TableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _cellArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeGiftCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeGiftCell"];
    cell.giftModel = _cellArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeGiftModel *giftModel = _cellArray[indexPath.row];
    
    _baseDVC = [[HomeBaseDetailViewController alloc]init];
    _baseDVC.ID = [NSString stringWithFormat:@"%d",[giftModel.ID intValue]];
    _baseDVC.hidesBottomBarWhenPushed = YES;
    [_naVC pushViewController:_baseDVC animated:YES];
    [self.navigationController pushViewController:_baseDVC animated:YES];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


#pragma mark - 数据请求

- (void)readScrollData
{
    NSString *url = kHomeWheel;
    [LORequestManger GET:url success:^(id response) {
        
        NSDictionary *dic = (NSDictionary *)response;
        NSArray *array = dic[@"data"][@"banners"];
        for (NSDictionary *newDic in array) {
            HomeWheelModel *wheelModel = [[HomeWheelModel alloc]init];
            [wheelModel setValuesForKeysWithDictionary:newDic];
            [self.wheelArray addObject:wheelModel];
        }
        
        _headView.headScrollArray = _wheelArray;
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error:%@",error);
    }];
}

- (void)readBeautyData
{
    [LORequestManger GET:kHomeBeauty success:^(id response) {
        
        NSDictionary *dic = (NSDictionary *)response;
        NSArray *array = dic[@"data"][@"promotions"];
        for (NSDictionary *newDic in array) {
            HomeBeautyModel *beauty = [[HomeBeautyModel alloc]init];
            [beauty setValuesForKeysWithDictionary:newDic];
            [self.beautyArray addObject:beauty];
            
        }
        
        _headView.headBeautyArray = _beautyArray;
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error:%@",error);
    }];
}

- (void)readCellData
{
    NSString *newUrl = [kHomeSelection stringByAppendingString:[NSString stringWithFormat:@"%d",_num]];
    [LORequestManger GET:newUrl success:^(id response) {
        
        if (_refreshDownLoadType == RefreshDownLoadTypeUp) {
            [_cellArray removeAllObjects];
        }
        
        NSDictionary *dic = (NSDictionary *)response;
        NSArray *items = dic[@"data"][@"items"];
        for (NSDictionary *newDic in items) {
            HomeGiftModel *giftModel = [[HomeGiftModel alloc]init];
            [giftModel setValuesForKeysWithDictionary:newDic];
            [self.cellArray addObject:giftModel];
        }
        
        [_tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"error:%@",error);
        
    }];
}

- (void)headDownLoad
{
    _num = 0;
    _refreshDownLoadType = RefreshDownLoadTypeUp;
    [self readCellData];
    [_tableView.header endRefreshing];
}

- (void)footDownLoad
{
    _num += 20;
    _refreshDownLoadType = RefreshDownLoadTypeDown;
    [self readCellData];
    [_tableView.footer endRefreshing];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
